package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.entity.AppDownloadEntity;
import com.migo.entity.TechnicalSupportTypeEntity;
import com.migo.service.TechnicalSupportService;
import com.migo.service.TechnicalSupportTypeService;
import com.migo.utils.R;
import com.migo.utils.redis.RedisCacheUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by Wes on 2019/1/15.
 */
@CrossOrigin
@RestController
@RequestMapping("/api")
public class ApiTechnicalSupportController extends ApiBaseController {

    @Autowired
    private TechnicalSupportTypeService technicalSupportTypeService;

    @Autowired
    private TechnicalSupportService technicalSupportService;

    @Autowired
    private RedisCacheUtil<List<AppDownloadEntity>> redisCacheUtil;

    /**
     * 查询技术支持类型表
     */
    @IgnoreAuth
    @RequestMapping("/queryTechnicalSupportTypeList")
    public R queryTechnicalSupportTypeList(String proId, String language) throws Exception {
        Map<String, Object> paramMap = new HashedMap();
        paramMap.put("proId", proId);
        paramMap.put("language", language);
        //缓存部分 开始
        List<TechnicalSupportTypeEntity> technicalSupportList = null;
        String key = "queryTechnicalSupportTypeList_" + proId + "_" + language;
        if (redisCacheUtil.hashKey(key)) {
            technicalSupportList = redisCacheUtil.getCacheList(key);
        } else {
            //查询数据库写入redis
            synchronized (this) {
                technicalSupportList = technicalSupportTypeService.queryList(paramMap);
                redisCacheUtil.setCacheList(key, technicalSupportList);
            }
        }
        //缓存部分 结束
        return R.ok().put("data", technicalSupportList);
    }

    /**
     * 查询技术支持列表
     */
    @IgnoreAuth
    @RequestMapping("/queryTechnicalSupportList")
    public R queryTechnicalSupportList(String typeId, String language) throws Exception {
        Map<String, Object> paramMap = new HashedMap();
        paramMap.put("typeId", typeId);
        return R.ok().put("data", technicalSupportService.queryList(paramMap));
    }


}
